<?php
class Formifier {
  public $model;
  public $action;
  public $object;
  
  public function __construct($model, $action, $options = array()) {
    $this->model = underscorify($model);
    $this->action = $action;
    $this->object = (isset($options['object'])) ? $options['object'] : null;
    
    $open_tag_options = array();
    foreach($options as $key => $vaue) {
      if($key == 'object') {
        continue;
      }
      $open_tag_options[$key] = $value;
    }
    
    $this->open_form_tag($open_tag_options);
  }
  
  public function open_form_tag($options) {
    $form = "\t<form%s>\n";
    $real_options = array();
    
    if(isset($options['action'])) {
      $real_options['action'] = $options['action'];
    }
    else {
      if($this->object != null and isset($this->object->id) and $this->object->id != 0) {
        $real_options['action'] = ROOT . $this->model . '/' . $this->object->id . '/' . $this->action;
      }
      else {
        $real_options['action'] = ROOT . $this->model . '/' . $this->action;
      }
    }
    $real_options['action'] .= (isset($options['to_script']) and $options['to_script'] === false) ? '' : '.htm';
    
    $real_options['method'] = (isset($options['method'])) ? $options['method'] : 'post';
    
    $real_options['id'] = (isset($options['id'])) ? $options['id'] : $this->action . '_' . $this->model . '_form';
    
    if(isset($options['multipart']) and $options['multipart'] === true) {
      $real_options['enctype'] = 'multipart/form-data';
    }
    
    foreach($options as $key => $value) {
      if($key == 'to_script' or $key == 'multipart' or $key == 'action' or $key == 'id' or $key == 'method') {
        continue;
      }
      $real_options[$key] = $value;
    }
    
    $attributes = "";
    foreach($real_options as $key => $value) {
      $attributes .= " $key=\"$value\"";
    }
    
    $form = sprintf($form, $attributes);
    
    echo $form;
  }
  
  public function close_form_tag() {
    $form = "\t</form>\n";
    
    echo $form;
  }
  
  public function destroy() {
    $this->close_form_tag();
    unset($this);
  }
  
  public function text($attribute, $value = null, $options = array()) {
    $input = "\t<input type=\"text\"%s />\n";
    
    $options['name'] = (isset($options['name'])) ? $options['name'] : $this->model . '[' . $attribute . ']';
    $options['value'] = ($this->object == null) ? $value : $this->object->{$attribute};
    $options['id'] = (isset($options['id'])) ? $options['id'] : $this->model . '_' . $attribute;
    $options['class'] = (isset($options['class'])) ? $options['class'] : 'txt';
    
    $attributes = "";
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
  }
  
  public function password($attribute, $value = null, $options = array()) {
    $input = "\t<input type=\"password\"%s />\n";
    
    $options['name'] = (isset($options['name'])) ? $options['name'] : $this->model . '[' . $attribute . ']';
    $options['value'] = ($this->object == null) ? $value : $this->object->{$attribute};
    $options['id'] = (isset($options['id'])) ? $options['id'] : $this->model . '_' . $attribute;
    $options['class'] = (isset($options['class'])) ? $options['class'] : 'txt';
    
    $attributes = "";
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
  }
  
  public function textarea($attribute, $value = null, $options = array()) {
    $textarea = "\t<textarea%s>%s</textarea>\n";
    
    $options['name'] = (isset($options['name'])) ? $options['name'] : $this->model . '[$attribute]';
    $options['id'] = (isset($options['id'])) ? $options['id'] : $this->model . '_' . $attribute;
    $value = ($this->object == null) ? $value : $this->object->{$attribute};
    
    $attributes = "";
    foreach($options as $key => $val) {
        $attributes .= " $key=\"$val\"";
    }
    $textarea = sprintf($textarea, $attributes, $value);
    
    echo $textarea;
  }
  
  public function checkbox($attribute, $value = false, $options = array()) {
    $input = "\t<input type=\"checkbox\"%s />\n";
    
    $options['name'] = (isset($options['name'])) ? $options['name'] : $this->model . '[' . $attribute . ']';
    $options['id'] = (isset($options['id'])) ? $options['id'] : $this->model . '_' . $attribute;
    $options['class'] = (isset($options['class'])) ? $options['class'] : 'chkbx';
    if($value == true) {
      $options['checked'] = 'checked';
    }
    
    $attributes = "";
    foreach($options as $key => $value) {
      $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
  }
  
  public function submit($value = 'Submit', $options = array()) {
    $input = "\t<input type=\"submit\"%s />\n";
    
    $options['name'] = (isset($options['name'])) ? $options['name'] : $this->model . '[form_submit]';
    $options['value'] = $value;
    $options['id'] = (isset($options['id'])) ? $options['id'] : $this->model . '_form_submit';
    $options['class'] = (isset($options['class'])) ? $options['class'] : 'btn';
    
    $attributes = "";
    foreach($options as $key => $value) {
        $attributes .= " $key=\"$value\"";
    }
    $input = sprintf($input, $attributes);
    
    echo $input;
  }
  
  public function label($attribute, $text, $options = array()) {
    $label = "\t<label for=\"%s\"%s>%s</label>\n";
    
    $for = (isset($options['for'])) ? $options['for'] : $this->model . '_' . $attribute;
    
    $attributes = "";
    foreach($options as $key => $value) {
        if($key == "for") {
            continue;
        }
        $attributes .= " $key=\"$value\"";
    }
    
    $label = sprintf($label, $for, $attributes, $text);
    
    echo $label;
  }
}
?>